package com.simple.admin.controller.system;

import com.simple.framework.constant.HttpStatus;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.framework.core.domain.vo.PagingVo;
import com.simple.system.annotation.SystemLog;
import com.simple.system.domain.dto.AddSysDictionaryDataDto;
import com.simple.system.domain.dto.AddSysDictionaryDto;
import com.simple.system.domain.dto.ListSysDictionaryDto;
import com.simple.system.domain.entity.SysDictionary;
import com.simple.system.domain.entity.SysDictionaryData;
import com.simple.system.enums.BusinessType;
import com.simple.system.service.SysDictionaryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Description 数据字典接口-控制层
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("sys_dictionary")
public class SysDictionaryController extends BaseController {

    private final SysDictionaryService sysDictionaryService;

    public SysDictionaryController(SysDictionaryService sysDictionaryService) {
        this.sysDictionaryService = sysDictionaryService;
    }

    /**
     * 分页获取数据字典列表
     * @param dto 查询条件
     * @return PagingVo<SysDictionary>
     */
    @GetMapping
    public R<PagingVo<SysDictionary>> list(ListSysDictionaryDto dto) {
        return success(sysDictionaryService.list(dto));
    }


    /**
     * 创建数据字典
     * @param dto 数据字典
     * @return Long
     */
    @SystemLog(title = "新增字典",businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> create(@RequestBody @Validated AddSysDictionaryDto dto) {
        return success(HttpStatus.CREATED, sysDictionaryService.crate(dto));
    }


    /**
     * 删除数据字典
     * @param id 数据字典id
     * @return Void
     */
    @SystemLog(title = "删除字典",businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        sysDictionaryService.delete(id);
        return success();
    }

    /**
     * 更新数据字典
     * @param id  数据字典id
     * @param dto 数据字典
     * @return Void
     */
    @SystemLog(title = "修改字典",businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody @Validated AddSysDictionaryDto dto) {
        sysDictionaryService.update(id, dto);
        return success();
    }

    /**
     * 获取数据字典内容
     *
     * @param type 数据字典类型
     * @return List<SysDictionaryData>
     */
    @GetMapping("data/{type}")
    public R<List<SysDictionaryData>> getDictionaryData(@PathVariable String type) {
        return success(sysDictionaryService.getDictionaryData(type));
    }

    /**
     * 新增数据字典内容
     *
     * @param dto 数据字典内容
     * @return Long
     */
    @SystemLog(title = "新增字典内容",businessType = BusinessType.INSERT)
    @PostMapping("data")
    public R<Long> createData(@RequestBody @Validated AddSysDictionaryDataDto dto) {
        return success(HttpStatus.CREATED, sysDictionaryService.createData(dto));
    }

    /**
     * 更新数据字典内容
     *
     * @param id  数据字典内容id
     * @param dto 数据字典内容
     * @return Void
     */
    @SystemLog(title = "修改字典内容",businessType = BusinessType.UPDATE)
    @PutMapping("data/{id}")
    public R<Void> updateData(@PathVariable Long id, @RequestBody @Validated AddSysDictionaryDataDto dto) {
        sysDictionaryService.updateData(id, dto);
        return success();
    }

    /**
     * 删除数据字典内容
     *
     * @param id 数据字典内容id
     * @return Void
     */
    @SystemLog(title = "删除字典内容",businessType = BusinessType.DELETE)
    @DeleteMapping("data/{id}")
    public R<Void> deleteData(@PathVariable Long id) {
        sysDictionaryService.deleteData(id);
        return success();
    }
}
